package com.longtop.perform.khyg.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.khyg.model.TKhygIndex;
import com.longtop.perform.khyg.service.IndexDataService;
import com.longtop.perform.khyg.service.ProjectService;

/**
 * 
 * @author 孙景弘
 * 
 */
public class IndexDataAction extends BaseAction {

	private IndexDataService service;
	private final String DATASET_PORJECT_INDEX = "Dataset_project_index";
	private final String DATASET_PROJECT_SEGMENT = "Dataset_project_segment";
	private final String DATASET_TASK_USER = "Dataset_task_user";
	private final String DATASET_INDEX_DATA = "Dataset_index_data";

	// 查询条件：
	Map<String, Object> queryMap = new HashMap<String, Object>();

	public IndexDataAction() {
		this.service = this.getBean(IndexDataService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		Dataset dataset_project_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		Dataset dataset_index_data = this.getDataset(context, DATASET_INDEX_DATA);
		if (!WebUtil.validateDatasetExist(this, dataset_project_segment, DATASET_PROJECT_SEGMENT) || !WebUtil.validateDatasetExist(this, dataset_project_index, DATASET_PORJECT_INDEX))
			return;

		String projectId = this.getParameter(context, "projectId");

		// 获取考核方案对应分段
		ProjectService projectService = this.getBean(ProjectService.BEAN_ID);
		List list = projectService.getProjectSegment(projectId);
		BeanUtil.listToDataset(list, dataset_project_segment, false);

		// 方案对应指标：
		queryMap.put("projectId", projectId);
		queryMap.put("indexTypeEqual", "1");// 1代表定性指标，只查找定性指标
		service.queryForPageByIbatis("khygProjectSql.projectIndexIdToName", null, queryMap, dataset_project_index, false);

		// 组装员工-定性指标明细DATASET：
		service.bulidUserDxIndexDataset(dataset_project_index, dataset_task_user);

	}

	/**
	 * 获取考核员工对应定性指标
	 * 
	 * @param context
	 */
	public void queryUserIndexData(IActionContext context) {

		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		String segmentId = this.getParameter(context, "segmentId");

		service.getUserIndexData(segmentId, dataset_task_user, context);
	}

	/**
	 * 保存员工-主观评分（定性指标）
	 * 
	 * @param context
	 */
	public void submitUserIndexData(IActionContext context) {
		Dataset dataset_task_user = this.getDataset(context, DATASET_TASK_USER);
		Dataset dataset_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		if (!WebUtil.validateDatasetChange(this, dataset_task_user, DATASET_TASK_USER))
			return;

		try {
			List<TKhygIndex> indexList = BeanUtil.datasetToList(dataset_project_index, TKhygIndex.class);
			List<Record> taskUserRecordList = dataset_task_user.getRecordList();
			SessionObject sessionObject = this.getSessionObject(context);

			service.saveUserIndexData(indexList, taskUserRecordList, sessionObject);

			this.hintInfoMessage("保存成功");
		} catch (Exception e) {
			this.hintErrorMessage("保存失败");
			e.printStackTrace();
		}

	}
}
